﻿<%@ Page Language="C#" %>

<%@ Import Namespace="IRL.Utilities" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web.Mail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    
    protected void Page_Load(object sender, EventArgs e)
    {
        int mid, rtype;
        if (Request.QueryString["mid"] != null || Request.QueryString["rtype"] != null)
        {
            mid = int.Parse(Request.QueryString["mid"].ToString());
            rtype = int.Parse(Request.QueryString["rtype"].ToString());
            if (!Page.IsPostBack)
            {
                object connStr = ConfigurationSettings.AppSettings["sqlConn"];
                using (SqlConnection conn = new SqlConnection(connStr.ToString()))
                {
                    SqlCommand cmd = new SqlCommand("sp_Select_Milestone", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@milestoneid", SqlDbType.Int).Value = mid;
                    conn.Open();
                    SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    if (dr.Read())
                    {
                        for (int i = 0; i < dr.FieldCount; i++)
                        {
                            ViewState.Add(dr.GetName(i), dr[i]);
                        }
                    }
                    dr.Close();
                    ViewState.Add("ReminderType", Lookup.GetText("sp_Select_cms_Lookup 'reminder_type', " + rtype + ", 1", conn));
                }
            }
        }
        else
        {
            // There no query string provided
            Response.Redirect("listReminders.aspx");
        }
    }

    protected void cmdSend_Click(object sender, EventArgs e)
    {
        MailMessage mailer = new MailMessage();        
        StringBuilder strBody = new StringBuilder();
        
        mailer.From = "CMS - Mailer<chanmann@indochinaresearch.com>";
        mailer.To = txtTo.Text;
        mailer.Subject = "CMS - Reminder";
        mailer.BodyFormat = MailFormat.Html;
        strBody.Append("<style>.blueL{color:#10147e;}</style>");
        strBody.Append("\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"6\" style=\"font-family:verdana;font-size:7.5pt;font-weight:bold;\">");
        strBody.Append("\n  <tr>");
        strBody.Append("\n    <td>Project name:</td><td class=\"blueL\">" + ViewState["ProjectName"].ToString() + "</td>");
        strBody.Append("\n    <td width=\"20\">&nbsp;</td>");
        strBody.Append("\n    <td>Client name:</td><td class=\"blueL\">" + ViewState["ClientName"].ToString() + "</td>");
        strBody.Append("\n  </tr>");
        strBody.Append("\n  <tr>");
        strBody.Append("\n    <td>Milestone type:</td><td class=\"blueL\">" + ViewState["MilestoneType"].ToString() + "</td>");
        strBody.Append("\n    <td width=\"20\">&nbsp;</td>");
        strBody.Append("\n    <td>Reminder type:</td><td class=\"blueL\">" + ViewState["ReminderType"].ToString() + "</td>");
        strBody.Append("\n  </tr>");
        strBody.Append("\n  <tr>");
        strBody.Append("\n    <td>Billing date:</td><td class=\"blueL\">" + String.Format("{0:dd-MMMM-yyyy}", ViewState["BillingDate"]) + "</td>");
        strBody.Append("\n    <td width=\"20\">&nbsp;</td>");
        strBody.Append("\n    <td>Payment date:</td><td class=\"blueL\">" + String.Format("{0:dd-MMMM-yyyy}", ViewState["PaymentDate"]) + "</td>");
        strBody.Append("\n  </tr>");
        strBody.Append("\n  <tr>");
        strBody.Append("\n    <td>Amount:</td><td class=\"blueL\">" + String.Format("{0:c}", ViewState["Amount"]) + "</td>");
        strBody.Append("\n    <td width=\"20\">&nbsp;</td>");
        strBody.Append("\n    <td>&nbsp;</td><td>&nbsp;</td>");
        strBody.Append("\n  </tr>");
        strBody.Append("\n</table>");
        mailer.Body = strBody.ToString();
        // send mail
        SmtpMail.SmtpServer = "exchange";
        SmtpMail.Send(mailer);
        // Close window
        string strScript = "<script language=\"javascript\">";
        strScript += "window.close();<";
        strScript += "/";
        strScript += "script>";
        this.RegisterClientScriptBlock("closeWindow", strScript);
        
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Send reminder</title>
    <link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0">
    <tr>
        <td colspan="4">
            <h3 class="blueL">
			Send reminder</h3>
			
			<img src="images/line.gif" width="383" height="2">			
        </td>
    </tr>
    
    <tr>
        <td width="10">&nbsp;</td>
        <td>
            <table class="gray" border="0" cellspacing="0" cellpadding="6" style="font-size:7.5pt;font-weight:bold;">
                <tr>
                    <td>Project name:</td>
                    <td class="blueL"><%=ViewState["ProjectName"].ToString() %></td>
                </tr>
                <tr>
                    <td>Milestone type:</td>
                    <td class="blueL"><%=ViewState["MilestoneType"].ToString() %></td>
                </tr>
                <tr>
                    <td>Billing date:</td>
                    <td class="blueL"><%=String.Format("{0:dd-MMMM-yyyy}", ViewState["BillingDate"])%></td>
                </tr>
                <tr>
                    <td>Amount:</td>
                    <td class="blueL"><%=String.Format("{0:c}", ViewState["Amount"]) %></td>
                </tr>
            </table>
        </td>
        <td width="20">&nbsp;</td>
        <td valign="top">
            <table class="gray" border="0" cellspacing="0" cellpadding="6" style="font-size:7.5pt;font-weight:bold;">
                <tr>
                    <td>Client name:</td>
                    <td class="blueL"><%=ViewState["ClientName"].ToString() %></td>
                </tr>
                <tr>
                    <td>Reminder type:</td>
                    <td class="blueL"><%=ViewState["ReminderType"].ToString() %></td>
                </tr>
                <tr>
                    <td>Payment date:</td>
                    <td class="blueL"><%=String.Format("{0:dd-MMMM-yyyy}", ViewState["PaymentDate"]) %></td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td colspan="4">&nbsp;</td>
    </tr>
    
</table>
<form id="frmMailer" runat="server">
    <table border="0" cellspacing="0" cellpadding="6" style="font-size:7.5pt">
        <tr>
            <td><b>Email Address:</b></td>
            <td>
                <asp:TextBox ID="txtTo" AutoCompleteType="Email" Columns="40" runat="server" />
            </td>            
            <td>
                <asp:Button ID="cmdSend" Text="Send" runat="server" OnClick="cmdSend_Click" />
            </td>
        </tr>
    </table>
</form>
</body>
</html>
